﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using StillVoiceCommon;

namespace TeamTime
{
    public partial class MasterPopup : System.Web.UI.MasterPage
    {
        public string LoggedInMessage { get { return lblUserName.Text; } set { lblUserName.Text = value; } }
        public Security sec;
        public string pagePermissionsMsg;

        protected void Page_Init(object sender, EventArgs e)
        {
            //if (Session["UserID"] == null && Request.LogonUserIdentity.Name.ToUpper().Contains("GARETHFLAPTOP"))
            //{
            //    Session["UserID"] = 1;
            //    Session["UserName"] = "Gareth";
            //}

            sec = new Security(sqlDB.getDBConnString());
            if (Request.RawUrl.ToLower().Contains("login.aspx"))
            {
                this.lblUserName.Visible = false;
                this.cmdLogout.Visible = false;
            }
            else
            {
                if (Session["UserID"] == null)
                {
                    Response.Redirect("login.aspx?URL=" + Request.RawUrl, true);
                }
                else
                {
                    this.lblUserName.Visible = true;
                    this.cmdLogout.Visible = true;
                    sec.UserID = Convert.ToInt32(Session["UserID"]);

                    this.LoggedInMessage = "You are logged in as " + Session["UserName"] + " - " + Session["CompanyName"];
                    //if (Session["EmployeeName"] != Session["UserName"])
                    //    this.lblUserName.Text += " on behalf of " + Session["EmployeeName"];
                    if (!Page.IsPostBack)
                    {
                        string s = Parent.Page.Request.RawUrl.Split('?')[0].ToLower();
                        s = s.Replace(@"/", "").Replace("tepims", "").Replace("tepweb", "").Replace("teptest", "").Replace("teptrain", "");
                        if (!sec.CanDo(s) && !s.Contains("errorpage"))
                            Response.Redirect("ErrorPage.aspx?msg=You do not have permissions to the following function:" +
                                                this.Parent.Page.Title + "(" + s + ")");


                    }
                }
            }

        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void cmdLogout_Click(System.Object sender, System.EventArgs e)
        {
            Session["UserID"] = null;
            Session.Remove("MenuDS");
            ////release this user's menu from Cache
            //if (Cache["Menu" + Session["UserID"]] != null)
            //        Cache["Menu" + Session["UserID"]] = null; 
            //FormsAuthentication.SignOut()
            Response.Redirect("login.aspx"); //, true)
        }

    }
}
